package com.cloudcc.ccTool;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * @author T.han
 * @date 2024/4/17 10:51
 * @project Quectel-CRM
 * @Description //TODO 
 */
public class SendEmail {
    public static CCService cs;
    public static UserInfo userInfo;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public SendEmail(UserInfo userInfo) {
        this.userInfo = userInfo;
        cs = new CCService(userInfo);
    }

    public static void main(String[] args) {
        SendEmail SendEmail = new SendEmail(userInfo);
        String[] toAddress = {"858084865@qq.com"};
        String[] ccAddress = null;
        String[] bccAddress =null;
        String subject = "测试邮件";
        String content = "测试邮件";
        boolean isText = true;
        SendEmail.sendMailFromSystem(toAddress, ccAddress, bccAddress, subject, content, isText);
        String str = "";
    }

    public void sendMailFromSystem(String toAddress,String[] ccAddress,String[] bccAddress,String subject
            ,String content,boolean isText){

        JSONObject parObj = new JSONObject();
        parObj.put("toAddress",toAddress);//接收人
        parObj.put("ccAddress",ccAddress);//抄送人
        parObj.put("bccAddress",bccAddress);//密送人
        parObj.put("subject",subject);//标题
        parObj.put("content",content);//内容
        parObj.put("isText",isText);//是否采用TXT方式发送  true/false
        parObj.put("apiCode","");//邮件模板API代码
        parObj.put("templateStr","");//模板变量
        System.out.println("parObj = " + parObj);
        JSONObject requestBody = new JSONObject();
        // 构建请求体，包含服务名称、对象API名称和要删除的数据
        requestBody.put("serviceName", "sendEmail");
        requestBody.put("data", parObj);
        try {
            // 执行删除操作，并获取操作结果
            String path = cs.getOrgApiUrl() + "/openApi/common";
            String operationResult = cs.CCPost(path, requestBody.toString());
            logger.info("发送邮件服务-结果："+operationResult);
            JSONObject rstData = JSONObject.parseObject(operationResult);
            if (rstData.getBoolean("result")) {
                // 如果结果为true，则返回操作结果
                logger.info("发送成功");
//                return "发送成功";
            }
        } catch (Exception e) {
            // 捕获异常，将异常信息作为操作结果返回
//            operationResult = e.getMessage();
            logger.error("发送邮件服务异常："+ e.getMessage());
        }
    }
    public void sendMailFromSystem(String[] toAddress,String[] ccAddress,String[] bccAddress,String subject
            ,String content,boolean isText){

        JSONObject parObj = new JSONObject();
        parObj.put("toAddress",toAddress);//接收人
        parObj.put("ccAddress",ccAddress);//抄送人
        parObj.put("bccAddress",bccAddress);//密送人
        parObj.put("subject",subject);//标题
        parObj.put("content",content);//内容
        parObj.put("isText",isText);//是否采用TXT方式发送  true/false
        parObj.put("apiCode","");//邮件模板API代码
        parObj.put("templateStr","");//模板变量
        System.out.println("parObj = " + parObj);
        JSONObject requestBody = new JSONObject();
        // 构建请求体，包含服务名称、对象API名称和要删除的数据
        requestBody.put("serviceName", "sendEmail");
        requestBody.put("data", parObj);
        try {
            // 执行删除操作，并获取操作结果
            String path = cs.getOrgApiUrl() + "/openApi/common";
            String operationResult = cs.CCPost(path, requestBody.toString());
            logger.info("发送邮件服务-结果："+operationResult);
            JSONObject rstData = JSONObject.parseObject(operationResult);
            if (rstData.getBoolean("result")) {
                // 如果结果为true，则返回操作结果
                logger.info("发送成功");
//                return "发送成功";
            }
        } catch (Exception e) {
            // 捕获异常，将异常信息作为操作结果返回
//            operationResult = e.getMessage();
            logger.error("发送邮件服务异常："+ e.getMessage());
        }
    }

    // 该方法平台已实现，本地可放心调用
    public void sendEmailNew(String emailtemplateid,String[] toAddress,String[] ccAddress,String[] bccAddress,String recordId){
        logger.info("发送邮件：\nemailtemplateid ->" + emailtemplateid+"\n recordId ->"+recordId+"\n toAddress ->" + Arrays.toString(toAddress)+"\n " +
                "ccAddress ->" + Arrays.toString(ccAddress)+"\n bccAddress->" + Arrays.toString(bccAddress));
    }


}